//深拷贝
function deepClone(obj) {
  if (typeof obj !== "object" || obj === null) {
    return obj;
  }
  const newObj = Array.isArray(obj) ? [] : {};
  for (const key in obj) {
    newObj[key] = deepClone(obj[key]);
  }

  return newObj;
}

//插件
export function myPiniaPluginOne() {
  return {
    secret: "myPiniaPluginOne", //给所有仓库添加一条全局属性
  };
}

//挂给所有仓库
export function myPiniaPluginTwo({ store }) {
  store.test = "this is a store";
}

//单独挂给一个仓库
export function myPiniaPluginThree({ store }) {
  if (store.$id === "counter") {
    return {
      name: "my name is myPiniaPluginThree",
    };
  }
}

//扩展方法 重置仓库状态 composition api 无法使用 store.$reset() 方法
export function myPiniaPluginFour({ store }) {
  //深拷贝
  const state = deepClone(store.$state);
  store.reset = () => {
    store.$patch(deepClone(state));
  };
}
